package com.sz.sensor.module.main;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.sz.framework.base.BaseActivity;
import com.sz.framework.base.BaseFragment;
import com.sz.framework.utils.BarUtil;
import com.sz.sensor.R;
import com.sz.sensor.module.me.MeFragment;
import com.sz.sensor.module.platform.PlatformFragment;
import com.sz.sensor.module.scan.ScanFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity {
    @BindView(R.id.fl_content)
    FrameLayout flContent;
    @BindView(R.id.bnb_nav)
    BottomNavigationBar bnbNav;

    @Override
    public int generateLayout() {
        return R.layout.activity_main;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtil.setStatusBarDarkIcon(this);
        initFragment();
    }




    private void initFragment() {
        final List<BaseFragment> fragments = new ArrayList<>();
        fragments.add(ScanFragment.newInstance());
        fragments.add(PlatformFragment.newInstance());
        fragments.add(MeFragment.newInstance());
        bnbNav.addItem(new BottomNavigationItem(R.drawable.icon_scan, R.string.scan)
                .setInactiveIconResource(R.drawable.icon_scan))
                .addItem(new BottomNavigationItem(R.drawable.icon_platform_sel, R.string.platform)
                        .setInactiveIconResource(R.drawable.icon_platform_un_sel))
                .addItem(new BottomNavigationItem(R.drawable.icon_me_sel, R.string.me)
                        .setInactiveIconResource(R.drawable.icon_me_un_sel))
                .initialise();
        bnbNav.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                showFragment(position, fragments);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
        showFragment(0, fragments);
    }


    private void showFragment(int index, List<BaseFragment> fragments) {
        Fragment fragment = fragments.get(index);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // 暂停当前tab
        fragment.onPause();

        if (!fragment.isAdded()) {
            ft.add(R.id.fl_content, fragment);
        }
        else {
            // 启动目标tab的onResume()
            fragment.onResume();
        }
        for (int i = 0; i < fragments.size(); i++) {
            if (index == i) {
                if (fragments.get(i).isHidden()) {
                    ft.show(fragments.get(i));
                }
            }
            else {
                if (!fragments.get(i).isHidden()) {
                    ft.hide(fragments.get(i));
                }
            }
        }
        ft.commitAllowingStateLoss();
    }

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
    }
}
